/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.ifrn.dao;

import br.edu.ifrn.dominio.Relatorio;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
/**
 *
 * @author Braga
 */
public class RelatorioDAO {
    public RelatorioDAO(){

    }

    public void insertRelatorio(Relatorio r) throws SQLException{
        Connection conn = ConnectionFactory.getConnection();

        String sql =  "INSERT INTO RELATORIO VALUES  (?, ?, ?, ?, ?, ?)";

        PreparedStatement stm = conn.prepareStatement(sql);

        stm.setInt(1, r.getID());
        stm.setString(2, r.getNucleo_Pesq());
        stm.setDate(3, r.getData_Exec());
        stm.setString(4, r.getNome());
        stm.setString(5, r.getPesquisador_CPF());

        stm.executeUpdate();
    }

    public void deleteRelatorio(int ID) throws SQLException{
        Connection conn = ConnectionFactory.getConnection();

        String sql = "DELETE RELATORIO WHERE ID = ?";

        PreparedStatement stm = conn.prepareStatement(sql);

        stm.setInt(1, ID);

        stm.executeUpdate();
    }

    public void updateRelatorio(int ID, Relatorio r) throws SQLException{
        Connection conn = ConnectionFactory.getConnection();

        String sql = "UPDATE SET NUCLEO_PESQ = ?, SET DATA_EXEC = ?, SET NOME = ?,"
                +" SET PESQUISADOR_CPF = ? WHERE ID = ?";

        PreparedStatement stm = conn.prepareStatement(sql);

        stm.setString(1, r.getNucleo_Pesq());
        stm.setDate(2, r.getData_Exec());
        stm.setString(3, r.getNome());
        stm.setString(4, r.getPesquisador_CPF());
        stm.setInt(5, ID);

        stm.executeUpdate();
    }

    public LinkedList selectRelatorio() throws SQLException{
        Connection conn = ConnectionFactory.getConnection();
        LinkedList list = new LinkedList();
        ResultSet rs = null;

        String sql = "SELECT * FROM RELATORIO";

        PreparedStatement stm = conn.prepareStatement(sql);

        rs = stm.executeQuery();

        while(rs.next()){
            Relatorio r = new Relatorio();

            r.setID(rs.getInt(1));
            r.setNucleo_Pesq(rs.getString(2));
            r.setData_Exec(rs.getDate(3));
            r.setNome(rs.getString(4));
            r.setPesquisador_CPF(rs.getString(5));

            list.add(r);
        }

        return list;
    }

    public LinkedList selectRelatorioById(int ID) throws SQLException{
        Connection conn = ConnectionFactory.getConnection();
        LinkedList list = new LinkedList();
        ResultSet rs = null;

        String sql = "SELECT * FROM RELATORIO WHERE ID = ?";

        PreparedStatement stm = conn.prepareStatement(sql);

        stm.setInt(1, ID);

        rs = stm.executeQuery();

        while(rs.next()){
            Relatorio r = new Relatorio();

            r.setID(rs.getInt(1));
            r.setNome(rs.getString(2));
            r.setData_Exec(rs.getDate(3));
            r.setNome(rs.getString(4));
            r.setPesquisador_CPF(rs.getString(5));

            list.add(r);
        }

        return list;
    }
}
